package cn.virens.web.components.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;

@Component
public class TestRabbitListener extends cn.virens.web.components.rabbit.RabbitListener {
	protected static final String queue = "queue-mq-online";

	@Override
	@RabbitListener(queues = queue)
	public void onMessage(Message message, Channel channel) throws Exception {
		super.onMessage(message, channel);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		mAmqpAdmin.declareQueue(new Queue(queue, false, false, true));
		mAmqpAdmin.declareExchange(new DirectExchange(exchange, false, true));
		mAmqpAdmin.declareBinding(new Binding(queue, DestinationType.QUEUE, exchange, "online", null));
	}

	@Override
	protected void onHandler(Message message, Channel channel, JSONObject json) {
		logger.debug("Receiver  : " + json.toJSONString());
	}

}
